package com.tarena.demo.luban.all.main.controller;


import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    private static final String ORDER_ADD_TOPIC="order_add_topic";
    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    public JsonResult addOrder(OrderAddParam orderAddParam){
        Message<OrderAddParam> message =
                MessageBuilder.withPayload(orderAddParam).build();
        rocketMQTemplate.asyncSend(ORDER_ADD_TOPIC, message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("成功发送");
            }
            @Override
            public void onException(Throwable e) {
                e.printStackTrace();
                System.out.println("发送失败");
            }
        });
        //如果下单的逻辑 采用的是削峰填谷，发送消息的结构，就无法满足立刻让前端用户获取下单结果
        return JsonResult.ok("下单完成!");
    }

}
